package com.xdap.keymen.service.dayang;

import com.xdap.keymen.entity.dayang.DaYangGroupBuyingEntity;
import com.xdap.keymen.entity.dayang.DaYangShopMapEntity;
import com.xdap.motor.entity.SnowflakeIdWorker;
import com.xdap.runtime.service.RuntimeAppContextService;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DaYangGroupBuyingService {
    private final RuntimeDatasourceService runtimeDatasourceService;
    private final RuntimeAppContextService runtimeAppContextService;
    private final SnowflakeIdWorker snowflakeIdWorker;
    private final DaYangShopMapService daYangShopMapService;

    public void saveOrUpdate(DaYangGroupBuyingEntity daYangGroupBuyingEntity) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        daYangGroupBuyingEntity.setTenant_id(currentTenantId);
        DaYangGroupBuyingEntity existedOrder = getByOrder(daYangGroupBuyingEntity.getGroup_buy_order());
        if (null != existedOrder && existedOrder.getMsg_id().equals(daYangGroupBuyingEntity.getMsg_id())) {
            daYangGroupBuyingEntity.setId(existedOrder.getId());
            daYangGroupBuyingEntity.setDocumentId(existedOrder.getDocumentId());
            runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId).
                    update("group_buy_order", daYangGroupBuyingEntity.getGroup_buy_order()).
                    update("amount", daYangGroupBuyingEntity.getAmount()).
                    update("order_status", daYangGroupBuyingEntity.getOrder_status()).
                    update("order_time", daYangGroupBuyingEntity.getOrder_time()).
                    update("payment_time", daYangGroupBuyingEntity.getPayment_time()).
                    update("redeem_store", daYangGroupBuyingEntity.getRedeem_store()).
                    update("redeem_user", daYangGroupBuyingEntity.getRedeem_user()).
                    update("redeem_time", daYangGroupBuyingEntity.getRedeem_time()).
                    update("certificate_id", daYangGroupBuyingEntity.getCertificate_id()).
                    update("pay_amount", daYangGroupBuyingEntity.getPayAmount()).
                    update("coupon_status", daYangGroupBuyingEntity.getCouponStatus()).
                    update("msg_id", daYangGroupBuyingEntity.getMsg_id()).
                    eq("id", existedOrder.getId()).doUpdate(daYangGroupBuyingEntity);
        } else {
            daYangGroupBuyingEntity.setId(snowflakeIdWorker.nextId());
            daYangGroupBuyingEntity.setDocumentId(snowflakeIdWorker.nextId());
            runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId).doInsert(daYangGroupBuyingEntity);
        }
    }

    /**
     * Get order from DB, because the coupon may be pushed twice, we need to update the data.
     *
     * @param order The orderId.
     * @return dyGroupBuyingEntity.
     */
    public DaYangGroupBuyingEntity getByOrder(String order) {
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        return runtimeDatasourceService.buildTenantMpaasQuery("xdap_app_" + currentTenantId)
                .eq("group_buy_order", order)
                .doQueryFirst(DaYangGroupBuyingEntity.class);
    }

    public DaYangGroupBuyingEntity buildGroupBuyingEntity(String orderId,String orderStatus,
                                                          String couponCode, String couponStatus,
                                                          Double couponAmount, Double couponPayAmount,
                                                          int couponCount) {

        String currentTime = DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd hh:mm:ss");
        DaYangGroupBuyingEntity daYangGroupBuyingEntity = new DaYangGroupBuyingEntity();
        //核销时间
        daYangGroupBuyingEntity.setRedeem_time(currentTime);
        //订单号
        daYangGroupBuyingEntity.setGroup_buy_order(orderId);
        //订单状态
        daYangGroupBuyingEntity.setOrder_status(orderStatus);
        //订单时间
        daYangGroupBuyingEntity.setOrder_time(currentTime);
        //团购券Code
        daYangGroupBuyingEntity.setCertificate_id(couponCode);
        //团购券金额
        daYangGroupBuyingEntity.setAmount(couponAmount);
        //团购券状态
        daYangGroupBuyingEntity.setCouponStatus(couponStatus);
        //团购券支付金额
        daYangGroupBuyingEntity.setPayAmount(couponPayAmount);
        //团购券数量（美团）
        daYangGroupBuyingEntity.setCount(couponCount);
        //支付时间
        daYangGroupBuyingEntity.setPayment_time(currentTime);
        daYangGroupBuyingEntity.setUsageStatus("1");
        daYangGroupBuyingEntity.setStatus("COMPLETED");
        return daYangGroupBuyingEntity;
    }

}
